/*
 * @(#)SageWriter.java $version 2012. 1. 31.
 *
 */

package com.mywep.myalimi.batch.sage;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import com.mywep.myalimi.model.Keyword;
import com.mywep.myalimi.model.KeywordAndContents;
import com.mywep.myalimi.model.NewContent;
import com.mywep.myalimi.repository.content.NewContentRepository;
import com.mywep.myalimi.repository.keyword.KeywordRepository;

/**
 * @author 한지웅
 */
public class SageWriter implements ItemWriter<KeywordAndContents> {
	private static final Logger LOG = LoggerFactory.getLogger(SageWriter.class);
	@Autowired
	private NewContentRepository newContentRepository;
	@Autowired
	private KeywordRepository keywordRepository;
	/**
	 * @param arg0
	 * @throws Exception
	 * @see org.springframework.batch.item.ItemWriter#write(java.util.List)
	 */
	@Override
	public void write(List<? extends KeywordAndContents> arg0) throws Exception {

		for (KeywordAndContents keywordAndContents : arg0) {
			/**
			 * newContent insert
			 */
			for (NewContent content : keywordAndContents.getContents()) {
				try {
					newContentRepository.insert(content);
				} catch (Exception e) {
					LOG.info(e.getMessage());
					continue;
				}
			}

			for (String keyword : keywordAndContents.getKeywords()) {
				Keyword param = new Keyword();
				param.setKeyword(keyword);
				param.setLastAggregateDate(DateUtils.addMinutes(Calendar.getInstance().getTime(), 10));
				keywordRepository.updatePrimaryKey(param);
			}

		}
		
		
		

	}

}
